#include "addfeedwindow.h"
#include "ui_addfeedwindow.h"
#include "mytcpsocket.h"
#include <QFileDialog>
#include <QMouseEvent>

AddFeedWindow::AddFeedWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddFeedWindow)
{
    ui->setupUi(this);

    // 窗口渲染
    setQssStyle(QString("../style/AddFeed.qss"),this);
    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    type = 95;  // 文本动态
    lab_img = new QLabel();
    lab_img->setFixedSize(100,100);
    lab_img->setScaledContents(true);
    lab_img->installEventFilter(this);

    connect(this,&AddFeedWindow::sig_announceFeed,this,[=](){
        // 封装动态报文
        QVariantMap map;
        map["cmd"]="announce_feed";
        map["announcer"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
        map["nick"] =  GlobalObject::getGlobalObject()->getUserInfo().sdata.nick;
        map["pyqtype"]=type;
        map["content"]=QString("%1#%2").arg(text).arg(GlobalObject::getGlobalObject()->getSaveFileSrvPath());

        //发送动态报文
        GlobalObject::getGlobalObject()->sendRequest(map);

        ui->textEdit->clear();
        this->hide();
    });
}

AddFeedWindow::~AddFeedWindow()
{
    delete ui;
}

void AddFeedWindow::on_btn_release_clicked()
{
    qDebug()<<"type:"<<type;
    // 获取发布内容
    text = ui->textEdit->toPlainText();

    // 如果是图文,那么将图片上传到服务器
    if(type == 96){
        GlobalObject::getGlobalObject()->setSendfilePath(imgpath);
        if(GlobalObject::getGlobalObject()->getSendfilePath().isNull()){
            qDebug()<<"GlobalObject::getGlobalObject()->getSendfilePath().isNull ";
            return;
        }

        //获取文件信息
        QFileInfo fileinfo(GlobalObject::getGlobalObject()->getSendfilePath());
        //文件名(带有后缀)
        QString fname = fileinfo.fileName();
        //文件大小(KB)
        qint64 fsize = fileinfo.size();

        QVariantMap map;
        map["cmd"] = "file_head";
        map["filename"] = fname;
        map["filesize"] = fsize;

        // 发送文件头
        GlobalObject::getGlobalObject()->sendRequest(map);
        // 上传动态文件
        GlobalObject::getGlobalObject()->setUpload_file_flag(3);
        return;
    }

    // 封装动态报文
    QVariantMap map;
    map["cmd"]="announce_feed";
    map["announcer"] = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;
    map["nick"] =  GlobalObject::getGlobalObject()->getUserInfo().sdata.nick;
    map["pyqtype"]=type;
    map["content"]=text;

    //发送动态报文
    GlobalObject::getGlobalObject()->sendRequest(map);

    ui->textEdit->clear();
    this->hide();

}


void AddFeedWindow::on_btn_close_clicked()
{
    this->close();
}


void AddFeedWindow::on_btn_min_clicked()
{
    this->showMinimized();
}


void AddFeedWindow::on_btn_addimg_clicked()
{
    imgpath = QFileDialog::getOpenFileName(this,"选择一个图片","../test","*.png *.jpg");
    if(imgpath.isNull())
        return;
    QPixmap img(imgpath);
    lab_img->setPixmap(img);
    ui->horizontalLayout->addWidget(lab_img);
    lab_img->show();
    qDebug()<<imgpath;
    type = 96;
}

bool AddFeedWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == lab_img && event->type() == QEvent::MouseButtonPress) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::RightButton) {
            // 在这里处理右键点击事件
            ui->horizontalLayout->removeWidget(lab_img);
            lab_img->hide();
            type = 95;
            return true; // 返回true表示事件已被处理，不再传递给其他对象
        }
    }

    // 对于其他事件或对象，继续标准事件处理
    return QObject::eventFilter(obj, event);
}
